package com.project.utils;

import com.project.server.domain.estates.TEstatesAssets;
import com.project.server.domain.garden.DeviceInfo;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author Sweey Zhang
 * @description
 * @date 2021/8/9 14:52
 */
public class Dom4jUtil {
    public static ArrayList parseDeviceInfo(String xml) {
        ArrayList<DeviceInfo> list = new ArrayList<DeviceInfo>();
        try {
            SAXReader reader = new SAXReader();//创建一个读取XML文件的对象
            File xmlFile = new File(xml);
            if (xmlFile.exists()) {
                Document document = reader.read(xmlFile);
                Element root = document.getRootElement();//获取根节点
                for (Iterator<Element> reRoot = root.elementIterator(); reRoot.hasNext(); ) {
                    Element childElement = reRoot.next();//获取子节点
                    if (!"Devices".equals(childElement.getName())) {
                        continue;
                    }
                    for (Iterator<Element> reChildElement = childElement.elementIterator(); reChildElement.hasNext(); ) {
                        DeviceInfo demoEntity = new DeviceInfo();
                        Element dom = reChildElement.next();//获取子节点的下一节点
                        demoEntity.setId(Integer.parseInt(dom.attribute("id").getValue()));
                        demoEntity.setDeviceIp(dom.attribute("deviceIp").getValue());
                        demoEntity.setDevicePort(dom.attribute("devicePort").getValue());
                        demoEntity.setIp(dom.attribute("ip").getValue());
                        demoEntity.setName(dom.attribute("name").getValue());
                        //status   1==在线  2==离线
                        demoEntity.setStatus(dom.attribute("status").getValue());
                        demoEntity.setPassword(dom.attribute("password").getValue());
                        demoEntity.setPort(dom.attribute("port").getValue());
                        demoEntity.setType(dom.attribute("type").getValue());
                        demoEntity.setUser(dom.attribute("user").getValue());
                        list.add(demoEntity);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static ArrayList parseAssetsInfo(String xml) {
        ArrayList<TEstatesAssets> list = new ArrayList<TEstatesAssets>();
        try {
            SAXReader reader = new SAXReader();//创建一个读取XML文件的对象
            File xmlFile = new File(xml);
            if (xmlFile.exists()) {
                Document document = reader.read(xmlFile);
                Element root = document.getRootElement();//获取根节点
                for (Iterator<Element> reRoot = root.elementIterator(); reRoot.hasNext(); ) {
                    Element childElement = reRoot.next();//获取子节点
                    if (!"Devices".equals(childElement.getName())) {
                        continue;
                    }
                    for (Iterator<Element> reChildElement = childElement.elementIterator(); reChildElement.hasNext(); ) {
                        TEstatesAssets demoEntity = new TEstatesAssets();
                        Element dom = reChildElement.next();//获取子节点的下一节点
                        demoEntity.setAssetsId(dom.attribute("id").getValue());
                        demoEntity.setAssetsIp(dom.attribute("deviceIp").getValue());
                        demoEntity.setAssetsPort(dom.attribute("devicePort").getValue());
                        demoEntity.setAssetsName(dom.attribute("name").getValue());
                        //status   1==在线  2==离线
                        demoEntity.setAssetsStatus(dom.attribute("status").getValue());
                        demoEntity.setAssetsPassword(dom.attribute("password").getValue());
                        demoEntity.setAssetsType(dom.attribute("type").getValue());
                        demoEntity.setAssetsAccount(dom.attribute("user").getValue());
                        demoEntity.setDelFlag("0");
                        list.add(demoEntity);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static ArrayList parseDeviceInfo(File file) {
        ArrayList<DeviceInfo> list = new ArrayList<DeviceInfo>();
        try {
            SAXReader reader = new SAXReader();//创建一个读取XML文件的对象
            Document document = reader.read(file);
            Element root = document.getRootElement();//获取根节点
            for (Iterator<Element> reRoot = root.elementIterator(); reRoot.hasNext(); ) {
                Element childElement = reRoot.next();//获取子节点
                if (!"Devices".equals(childElement.getName())) {
                    continue;
                }
                for (Iterator<Element> reChildElement = childElement.elementIterator(); reChildElement.hasNext(); ) {
                    DeviceInfo demoEntity = new DeviceInfo();
                    Element dom = reChildElement.next();//获取子节点的下一节点
                    demoEntity.setId(Integer.parseInt(dom.attribute("id").getValue()));
                    demoEntity.setDeviceIp(dom.attribute("deviceIp").getValue());
                    demoEntity.setDevicePort(dom.attribute("devicePort").getValue());
                    demoEntity.setIp(dom.attribute("ip").getValue());
                    demoEntity.setName(dom.attribute("name").getValue());
                    demoEntity.setPassword(dom.attribute("password").getValue());
                    demoEntity.setPort(dom.attribute("port").getValue());
                    demoEntity.setType(dom.attribute("type").getValue());
                    demoEntity.setUser(dom.attribute("user").getValue());
                    list.add(demoEntity);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
